home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / parse.l < prev    next >
Text File  |  1997-12-07  |  22KB  |  425 lines

  1. W                       [ \t]
  2. N                       \r*\n
  3. D                       [0-9]
  4. L                       [a-zA-Z_]
  5. H                       [a-fA-F0-9]
  6. E                       [Ee][+-]?{D}+
  7. FS                      (f|F|l|L)
  8. IS                      (u|U|l|L)*
  9.  
  10. %x START_COMMENT     COMMENT     SPECIAL_COMMENT
  11. %x START_COMMENT_CPP COMMENT_CPP SPECIAL_COMMENT_CPP
  12. %x CPP CPP_START
  13. %x CPP_INCLUDE CPP_INC_FILE CPP_INC_FLAGS
  14. %x CPP_DEFINE CPP_DEFINE_ARGP CPP_DEFINE_BODY CPP_DEFINE_ARGS
  15. %x GNU_LABEL GNU_ATTRIBUTE GNU_EXTENSION GNU_TYPEOF
  16.  
  17. %{
  18. /***************************************
  19.   $Header: /home/amb/cxref/RCS/parse.l 1.23 1997/11/20 19:57:40 amb Exp $
  20.  
  21.   C Cross Referencing & Documentation tool. Version 1.4a.
  22.  
  23.   C lexical analyser
  24.   CPP processing, including GNU extensions, using yylval as a string.
  25.   ******************/ /******************
  26.   Written by Andrew M. Bishop
  27.  
  28.   This file Copyright 1995,96,97 Andrew M. Bishop
  29.   It may be distributed under the GNU Public License, version 2, or
  30.   any higher version.  See section COPYING of the GNU Public license
  31.   for conditions under which this file may be redistributed.
  32.   ***************************************/
  33.  
  34. #include "parse-yy.h"
  35. #include "parse-yacc.h"
  36. #include "cxref.h"
  37. #include "memory.h"
  38.  
  39. /*+ The name of the current file. +*/
  40. char* parse_file=NULL;
  41.  
  42. /*+ The current line number in the file. +*/
  43. int parse_line=0;
  44.  
  45.  
  46. /*+ If we are in a header file then ignore the comments. +*/
  47. extern int in_header;
  48.  
  49. /*+ One of the options controlling how comments are processed, +*/
  50. extern int option_all_comments,       /*+ use all comments not just the specially formattted ones. +*/
  51.            option_block_comments,     /*+ remove the leading block comment marker. +*/
  52.            option_no_comments;        /*+ ignore all comments. +*/
  53.  
  54. /*+ Flag that indicates if the comment warnings are to be issued. +*/
  55. extern int option_warn;
  56.  
  57.  
  58. /*+ The flags that come out of GCC when a file is included. +*/
  59. static int inc_file_flags=0;
  60.  
  61. /*+ The value of the thing that is defined (but only if it is simple). +*/
  62. static char* define_value=NULL;
  63.  
  64. /*+ The lex state at the time that a comment is seen. +*/
  65. static int comment_init_state=INITIAL;
  66.  
  67. /*+ To get around the GCC __attribute__ keyword, skip over matched () counted by this. +*/
  68. static int gnu_att_depth=0;
  69.  
  70. /*+ To get around the GCC __typeof__ keyword, skip over matched () counted by this. +*/
  71. static int gnu_typ_depth=0;
  72.  
  73. /*+ If we see a comment immediately after a ',', ';', '};', '},' or ')' then push it before. +*/
  74. static int push_past=0;
  75.  
  76.  
  77. /*++++++++++++++++++++++++++++++++++++++
  78.   Reset the Lexer, ready for the next file.
  79.   ++++++++++++++++++++++++++++++++++++++*/
  80.  
  81. void ResetLexer(void)
  82. {
  83.  parse_file=NULL;
  84.  parse_line=0;
  85.  inc_file_flags=0;
  86.  define_value=NULL;
  87.  comment_init_state=INITIAL;
  88.  gnu_att_depth=0;
  89.  gnu_typ_depth=0;
  90.  push_past=0;
  91.  
  92.  SeenComment((char*)3);
  93. }
  94.  
  95. %}
  96.  
  97. %%
  98.  
  99.  /* Comments, could be embedded in a preprocessor directive. */
  100.  
  101. <INITIAL>("/*"|"//")              { comment_init_state = INITIAL        ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  102. <CPP_START>("/*"|"//")            { comment_init_state = CPP_START      ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  103. <CPP>("/*"|"//")                  { comment_init_state = CPP            ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  104. <CPP_DEFINE>("/*"|"//")           { comment_init_state = CPP_DEFINE     ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  105. <CPP_INCLUDE>("/*"|"//")          { comment_init_state = CPP_INCLUDE    ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  106. <CPP_DEFINE_ARGP>("/*"|"//")      { comment_init_state = CPP_DEFINE_ARGP; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  107. <CPP_DEFINE_ARGS>("/*"|"//")      { comment_init_state = CPP_DEFINE_ARGS; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  108. <CPP_DEFINE_BODY>("/*"|"//")      { comment_init_state = CPP_DEFINE_BODY; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  109. <CPP_INC_FILE>("/*"|"//")         { comment_init_state = CPP_INC_FILE   ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  110. <CPP_INC_FLAGS>("/*"|"//")        { comment_init_state = CPP_INC_FLAGS  ; BEGIN(yytext[1]=='*'?START_COMMENT:START_COMMENT_CPP); }
  111.  
  112. <START_COMMENT>"*"+               { BEGIN(SPECIAL_COMMENT); }
  113. <START_COMMENT>"+"+               { BEGIN(SPECIAL_COMMENT); }
  114. <START_COMMENT>[*+ \t]*"*/"       { BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  115. <START_COMMENT>{N}                { if(option_all_comments) SeenComment(yytext); parse_line++;
  116.                                     if(option_all_comments) BEGIN(SPECIAL_COMMENT); else BEGIN(COMMENT); }
  117. <START_COMMENT>[^*+]              { if(option_all_comments) SeenComment(yytext);
  118.                                     if(option_all_comments) BEGIN(SPECIAL_COMMENT); else BEGIN(COMMENT); }
  119.  
  120. <START_COMMENT_CPP>"*"+           { BEGIN(SPECIAL_COMMENT_CPP); }
  121. <START_COMMENT_CPP>"+"+           { BEGIN(SPECIAL_COMMENT_CPP); }
  122. <START_COMMENT_CPP>{N}            { if(comment_init_state==CPP_INCLUDE || comment_init_state==CPP_DEFINE) comment_init_state=INITIAL;
  123.                                     parse_line++; BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  124. <START_COMMENT_CPP>[^*+]          { if(option_all_comments) BEGIN(SPECIAL_COMMENT_CPP); else BEGIN(COMMENT_CPP); }
  125.  
  126. <COMMENT>{N}                      { parse_line++; }
  127. <COMMENT>.                        { }
  128. <COMMENT>[+*]+/"*/"               { if(option_warn&COMMENT) fprintf(stderr,"%s:%d: Warning unbalanced cxref comment; starts simple, ends special.\n",parse_file,parse_line); }
  129. <COMMENT>"*/"                     { BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  130.  
  131. <COMMENT_CPP>.*{N}                { if(comment_init_state==CPP_INCLUDE || comment_init_state==CPP_DEFINE) comment_init_state=INITIAL;
  132.                                     parse_line++; BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  133.  
  134. <SPECIAL_COMMENT>{N}              { if(!option_no_comments) SeenComment(yytext); parse_line++; }
  135. <SPECIAL_COMMENT>{N}{W}*[+*|:]/({W}|{N}) { parse_line++;
  136.                                     if(option_block_comments) yytext[0]='\n',yytext[1]=0;
  137.                                     if(!option_no_comments) SeenComment(yytext); }
  138. <SPECIAL_COMMENT>[^\r\n*+]+       { if(!option_no_comments) SeenComment(yytext); }
  139. <SPECIAL_COMMENT>.                { if(!option_no_comments) SeenComment(yytext); }
  140.  
  141. <SPECIAL_COMMENT>{W}*"*"+"*/"     { if(!option_no_comments) SeenComment((char*)0);
  142.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  143. <SPECIAL_COMMENT>{W}*"+"+"*/"     { if(!option_no_comments) SeenComment((char*)1);
  144.                                     if(!in_header && comment_init_state==CPP_DEFINE_ARGS) SeenDefineFuncArgComment();
  145.                                     if(!in_header && comment_init_state==CPP_DEFINE_BODY) SeenDefineComment();
  146.                                     if(!in_header && comment_init_state==CPP_INCLUDE)     SeenIncludeComment();
  147.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  148. <SPECIAL_COMMENT>"*/"             { if(!option_all_comments && option_warn&COMMENT)
  149.                                        fprintf(stderr,"%s:%d: Warning unbalanced cxref comment; starts special, ends simple.\n",parse_file,parse_line);
  150.                                     if(option_all_comments) SeenComment((char*)2); else if(!option_no_comments) SeenComment((char*)1);
  151.                                     if(!in_header && comment_init_state==CPP_DEFINE_ARGS) SeenDefineFuncArgComment();
  152.                                     if(!in_header && comment_init_state==CPP_DEFINE_BODY) SeenDefineComment();
  153.                                     if(!in_header && comment_init_state==CPP_INCLUDE)     SeenIncludeComment();
  154.                                     BEGIN(comment_init_state); while(push_past) {push_past--;unput(yylval[push_past]);} }
  155.  
  156. <SPECIAL_COMMENT_CPP>[^\r\